// pages/onlineGarage/onlineGarage.js
var app = getApp();

import {
  getBrandList,
  getCarList
} from '../../api/onlineGarage'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarindex:0,
    tabdatalist:[],
    tabactive:0,
    navdata:{
      loading: false,
      color: '#DCAC44',
      background: '#f8f8f8',
      show: false,
      animated: false
    },
    carInfo:[],
    page:1,
    size:10,
    car_style_id:''

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.height,'heiheihie')
    this.setData({
      height:app.globalData.height,
      activeKey: options.flag ? options.flag : 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      activeKey: 0
    })
    // this.getBrand();
    // this.getCar(0);
    // this.getCar(1);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.carStyle()
    this.carlist()
  },
  school(){
    let that = this;
    if(that.data.getlock) return
    that.data.page++;
    that.carlist()
  },
  jump_goods(e){
    let id = e.currentTarget.dataset.id;
    console.log(id,'idididiidi')
    wx.navigateTo({
      url: `/pages/goods/goods?id=${id}`,
    })
  },
  tabbarfn(e){
    let i = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    let that = this;
    console.log(id,'ididididi')
    this.setData({
      tabbarindex:i,
      car_style_id:id,
      tabbarfn:false,
      getlock:false,
      page:1,
      carInfo:[]
      
    },(()=>{
      
     that.carlist()
    }))
    console.log(this.tabbarindex,'---',i)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  carlist(){
    let that = this;
    getCarList({
      data:{
        page:that.data.page,
        size:that.data.size,
        car_style_id:that.data.car_style_id
      }
    })
    .then((res)=>{
      let cardata = that.data.carInfo;
      let rescar = res.data.data;
      if(!rescar.length) {
        that.setData({
          getlock:true
        })
        return
      } 
      that.setData({
        carInfo:cardata.concat(rescar)
      })
    })
  },
  carStyle(){
    getBrandList({})
    .then((res)=>{
      console.log(res,'rrrrr')
      let tabarr = res.data;
      tabarr.unshift({
        id:'',
        name:"全部"
      })
      
      this.setData({
        tabdatalist:tabarr
      })
      console.log(this.data.tabdatalist,'tabdatalisttabdatalist')
    })
  },
  scrolltolower() {
    this.data.pageNum++;
    wx.showNavigationBarLoading();
    this.getCar(this.data.blokindex)
    // getCarList({
    //   data: {
    //     brandId: this.data.brandIds,
    //     pageNum: this.data.pageNum,
    //     pageSize: this.data.pageSize,
    //     useNature: this.data.activeKey - 1
    //   }
    // }).then(e => {
      wx.hideNavigationBarLoading();
    //   if (e.data.carList.length == 0) return wx.showToast({
    //     title: '没有更多了～～',
    //     icon: 'none'
    //   })
    //   var goodsList = this.data.goodsList;

    //   e.data.carList.forEach(e => {
    //     goodsList.push({
    //       imgAddr: e.carImg,
    //       brandName: e.brand + " " + e.carType,
    //       id: e.id,
    //       price: e.price,
    //       carLables: e.carLables,
    //       useNature:e.useNature
    //     })
    //   })
    //   this.setData({
    //     goodsList: goodsList
    //   })

    // })
  },
  /**
   * 页面上拉触底事件的处理函数
   */

  onReachBottom: function () {

  },

})